
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;



public class SpriteImage
{
	Image image;
	int width;
	int height;
	int frameWidth;
	int currentFrame;
	int FPS = 10;
	long lastTime = 0l;
	int frames = 0;
	boolean done = false;
	boolean pause = false;	

	public SpriteImage(Image image, int frames)
	{
		this.image = image;
		this.width = image.getWidth(null);
		this.height = image.getHeight(null);
		frameWidth = width / frames;
		this.frames = frames;
		currentFrame = 0;
	}

	/**
	 * Get the width of the drawn sprite
	 * 
	 * @return The width in pixels of this sprite
	 */
	public int getWidth()
	{
		return width;
	}

	/**
	 * Get the height of the drawn sprite
	 * 
	 * @return The height in pixels of this sprite
	 */
	public int getHeight()
	{
		return height;
	}

	/**
	 * Draw the sprite onto the graphics context provided
	 * 
	 * @param x The x location at which to draw the sprite
	 * @param y The y location at which to draw the sprite
	 */
	public void setFrame(int i)
	{
		if (i < frames)
		{
			currentFrame = i;
		}
	}

	public void changeSprite(Image i, int frames)
	{
		this.image = i;
		this.width = image.getWidth(null);
		this.height = image.getHeight(null);
		frameWidth = width / frames;
		this.frames = frames;
		currentFrame = 0;
	}
	public void draw(Graphics2D g,int x, int y)
	{
		g.drawImage(image, x, y, x + frameWidth, y + height, 
				currentFrame * frameWidth, 0, (currentFrame+1) * frameWidth , height, null);
		if (System.currentTimeMillis() - lastTime >= 1000/FPS)
		{
			lastTime = System.currentTimeMillis();
			if (!pause)
			{
				currentFrame++;
			}
			if (currentFrame >= frames)
			{
				currentFrame = 0;
				done = true;
			}
		}
	}
}